LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2008, 07:39 PM   #1
haydenyoung
LQ Newbie
 
Registered: Nov 2004
Location: Perth, WA, Australia
Distribution: Ubuntu Dapper Drake, CentOS4
Posts: 13

Rep: Reputation: 0
Adding dynamic command parameters in a Bash script


I currently have a bash script which loops the results of the find command;

Code:
for k in `find $assets -iname $j -type f`
but would like to allow users to set a no-recursive parameter which will add the -maxdepth parameter to the above find command;

Code:
...
[ "$recurse" == "0" ] && maxdepth='-maxdepth 1'
...
for k in `find $assets $maxdepth -iname $j -type f`
Unfortunately, the above yields the error;

find: invalid predicate `-maxdepth=1'

However, setting two variables;

Code:
...
if [ "$recurse" == "0" ]
then
maxdepth_param='-maxdepth'
maxdepth_val='1'
fi
...
for k in `find $assets $maxdepth_param $maxdepth_val -iname $j -type f`
works perfectly but seems extraneous.

Is this the only way to accomplish this or is there a cleaner solution.

Any help much appreciated.
 
Old 08-28-2008, 08:29 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Code:
...
[ "$recurse" == "0" ] && maxdepth='-maxdepth 1'
...
for k in `eval find $assets $maxdepth -iname $j -type f`
should work.
 
Old 08-28-2008, 08:31 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Try adding eval before find.
ta0kira

PS This board needs auto refresh every 1 second
 
Old 08-28-2008, 11:20 PM   #4
haydenyoung
LQ Newbie
 
Registered: Nov 2004
Location: Perth, WA, Australia
Distribution: Ubuntu Dapper Drake, CentOS4
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks all. Yep eval worked;

Code:
...
[ "$recurse" == "0" ] && maxdepth='-maxdepth 1'
...
for k in `eval find $assets $maxdepth -iname $j -type f`
 
  


Reply

Tags
bash, find



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script - parsing optional parameters yitzle Programming 5 02-17-2008 11:16 AM
BASH: run command with parameters from variable Roger Krowiak Programming 3 10-30-2007 03:27 AM
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 06:41 AM
bash script ? -- spaces in passed parameters azwr Linux - Newbie 3 06-18-2004 06:57 PM
Passing Parameters to Bash Script mooreted Linux - Software 3 04-05-2004 09:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration